package com.mywang.weitravel.net.interceptor;

import com.mywang.weitravel.base.MyApplication;
import com.mywang.weitravel.common.Contacts;
import com.mywang.weitravel.model.UserBean;
import com.mywang.weitravel.util.PreferencesUtil;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Desc: 请求头拦截器
 * Created by fww on 2019/3/29
 */
public class HeadRequestInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request()
                .newBuilder()
                .addHeader("X-APP-TYPE", "android");
        Object userInfo = PreferencesUtil.getInstance(MyApplication.getApplication()).getObject(Contacts.SP_USER_INFO);
        if (userInfo instanceof UserBean && PreferencesUtil.getInstance(MyApplication.getApplication()).isLogin()) {
            builder.addHeader("TOKEN", ((UserBean) userInfo).getToken());
        }
        Response response = chain.proceed(builder.build());
        return response;
    }
}
